package com.warren.biz.entity;

import com.baomidou.mybatisplus.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * <p>
 * 
 * </p>
 *
 * @author warren
 * @since 2021-04-20
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("biz_product_category")
@ApiModel(value="ProductCategory对象", description="")
public class ProductCategory implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "类别id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "类别名称")
    private String name;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "排序")
    private Integer sort;

    @ApiModelProperty(value = "物资分类创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @ApiModelProperty(value = "物资分类改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date modifiedTime;

    @ApiModelProperty(value = "父级分类id")
    private Long pid;

    @ApiModelProperty(value = "子菜单")
    //非数据库字段
    @TableField(select = false,exist = false)
    private List<ProductCategory> children = new ArrayList<>();


    @Override
    protected ProductCategory clone() throws CloneNotSupportedException {
        return (ProductCategory)super.clone();
    }

    public void add(ProductCategory e) throws CloneNotSupportedException {
        children.add(e);
    }

    /*
     * 排序,根据order排序
     */
    public static Comparator<ProductCategory> order(){
        return (o1, o2) -> {
            if(!o1.getSort().equals(o2.getSort())){
                return (int) (o1.getSort() - o2.getSort());
            }
            return 0;
        };
    }
}
